<?php
namespace common\tools;
/**
 * Created by PhpStorm.
 * User: steven
 * Date: 2017/9/13
 * Time: 下午2:37
 */
class Logger
{

    const LEVEL_A = 1;
    const LEVEL_B = 2;
    const LEVEL_C = 3;

    /**
     * @param $name
     * @return Logger
     */
    public static function instance($name)
    {
        return new Logger($name);
    }

    private function __construct($name)
    {
        $this->name = $name;
        $this->config = Config::get('log');
        $this->ip = Ip::getIp();
    }

    /**
     * @param $name
     * @param string $info
     * @param $level
     * @return mixed
     */
    public static function debug($name, $info = '', $level = self::LEVEL_C)
    {
        static $debug = array();
        if (!isset($debug[$name])) {
            $debug[$name] = self::instance('debug.' . $name);
        }
        $info = print_r($info, 1);
        //这里的堆栈多了一层
        $traceLevel = 2;
        $debug[$name]->log($info, $level, $traceLevel);
        return $debug[$name];
    }


    /**
     * 写入log
     * @param $info
     * @param $level
     * @param int $traceLevel
     */
    public function log($info, $level = self::LEVEL_C, $traceLevel = 1)
    {
        if (!is_array($info)) {
            $info = array('msg' => $info);
        }
        if (!empty($info['timestamp'])) {
            $info['@timestamp'] = date('c', strtotime($info['timestamp']));
            unset($info['timestamp']);
        } else {
            $info['@timestamp'] = date('c');
        }
        if(empty($info['msg'])){
            $info['msg']='';
        }



        //session id
        $r = $trace = debug_backtrace();
        $info['caller'] = '';
        if (isset($r[$traceLevel]['function'])) {
            $tag = $r[$traceLevel]['function'] . '()';
            if (isset($r[$traceLevel]['class'])) {
                $tag = $r[$traceLevel]['class'] . '::' . $tag;

            }
            $info['caller'] = $tag;
        }
        $info['host'] = gethostname();
        $info['client_ip'] = $this->ip;
        $info['domain'] = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';

        $info['_type'] = $this->name;
        $info['level'] = $level;


        //消息的格式
        $callerShort=basename(str_replace('\\','/',$info['caller']));
        $pid = posix_getpid();
        $msg = "{$info['@timestamp']}\tPID:$pid\t{$info['_type']}\t{$callerShort}\t{$info['msg']}\n";
        $is_debug=substr($this->name, 0, 5) == 'debug';

        //是否把消息写入控制台
        if (!empty($this->config['debug']) && $this->config['debug'] == 'stderr') {
            if ($is_debug) {
                file_put_contents('php://stderr', $msg);
            }
        }

        //是否把消息写入文件
        if (!empty($this->config['logfile'])) {
            $filename = '/tmp/log.' . $this->name . '.' . date('Y-m-d');
            $create = !file_exists($filename);
            file_put_contents($filename, $msg, FILE_APPEND);
            //error_log($msg,3,$filename);
            if ($create) {
                chmod($filename, 0777);
            }
        }

        //调试不发送到日志服务
        if($is_debug){
            return;
        }

    }
}